<?php

	/**
	 * Workspace@ Engine application comment!
	 * 
	 * LICENSE
	 * This application is licensed under New BSD license.
	 * Please read complete LICENSE.txt inside of root folder for more details.
	 * 
	 * We wish you good building :) 
	 * 
	 * @category   Application
	 * @package    Bootstrap
	 * @author     Nevio Vesic <nevio@workspaceat.com>
	 * @copyright  2008-2009 Workspace@ group in association with Design strategist
	 */
	
	/**
	 * @see Engine_Application_Abstract
	 */
	require_once 'Application/Abstract.php';
	
	/**
	 * Enter description here...
	 *
	 */
	class Engine_Application_Page extends Engine_Application_Abstract 
	{
		/**
		 * Current object instance
		 *
		 * @var Object[this]
		 */
		protected static $instance = null;
		
		/**
		 * Data....
		 *
		 * @var array()
		 */
		protected static $data = null;
		
		/**
		 * Base theme path
		 *
		 * @var string
		 */
		public    static $themepath = null;
		
		/**
		 * Page php file.. ( layout file constant )
		 *
		 */
		const PAGE_EXT     = ".phtml";
		

		
		/**
		 * Create instance
		 *
		 * @return Object[self]
		 */
		public static function getInstance( $data )
		{
			if ( self::$data )
			{
				self::$data = null;
			}
			
			# ok lets find out path
			if ( self::$themepath )
			{
				self::$themepath = null;
			}
			
			self::$themepath = self::$plugins -> themes -> getThemePath();
			self::$data = parent::getLocationData();
			
			if (! self::$instance )
				self::$instance = new self ( $data );
				
			return self::$instance;
		}
		
		/**
		 * Dispatch requested page... Throw error if current page
		 * doesn't exists!
		 */
		public function dispatch()
		{
			$plugins = self::$plugins;
			$repository = self::$repository;
			$params     = self::$data["params"];
			
			if ( self::$system -> authentication -> activated )
			{
				
				if (! self::$plugins -> users-> isAuthenticated() ){
					require_once self::$themepath . self::$system -> authentication -> page . self::PAGE_EXT;
				}
				
				$a = self::$data['elements'];
				
				if( $a[0] )
				{
					$i = 0;
					foreach ( $a as $key )
					{
						if ( $a [ $i+1 ] )
						{
							$b .= $key . DIRECTORY_SEPARATOR;
						}
						else{
							$b .= $key;
						}
						++$i;
					}
					
					$i = $i - 1;
					
					if (! fopen ( self::$themepath . $b . self::PAGE_EXT, "r", true ) )
					{
						throw new Engine_Exception( "Page '{$a[$i]}' can't be resolved. It doesn't exists!" );
					}
					
					require_once self::$themepath . $b . self::PAGE_EXT;
					
				}else 
				{
					if (! @fopen ( self::$themepath . self::$system -> start -> page . self::PAGE_EXT, "r", true ) )
					{
						throw new Engine_Exception( "Page '{$a[$i]}' can't be resolved. It doesn't exists!" );
					}
					
					require_once self::$themepath . self::$system -> start -> page . self::PAGE_EXT;
				}	
			}
			else
			{
				$a = self::$data['elements'];
				if( $a[0] )
				{
					$i = 0;
					foreach ( $a as $key )
					{
						if ( $a [ $i+1 ] )
						{
							$b .= $key . DIRECTORY_SEPARATOR;
						}
						else{
							$b .= $key;
						}
						++$i;
					}
					
					$i = $i - 1;
					
					if (! @fopen ( self::$themepath . $b . self::PAGE_EXT, "r", true ) )
					{
						throw new Engine_Exception( "Page '{$a[$i]}' can't be resolved. It doesn't exists!" );
					}
					
					require_once self::$themepath . $b . self::PAGE_EXT;
					
				}else 
				{
					if (! @fopen ( self::$themepath . self::$system -> start -> page . self::PAGE_EXT, "r", true ) )
					{
						throw new Engine_Exception( "Page '{$a[$i]}' can't be resolved. It doesn't exists!" );
					}
					
					require_once self::$themepath . self::$system -> start -> page . self::PAGE_EXT;
				}
			}
		}
		
		public function getDefaultPage()
		{
			if (! @fopen ( self::$themepath . self::$system -> start -> page . self::PAGE_EXT, "r", true ) )
			{
				throw new Engine_Exception( "Page '{$a[$i]}' can't be resolved. It doesn't exists!" );
			}
			
			require_once self::$themepath . self::$system -> start -> page . self::PAGE_EXT;
		}
		


	}

